<?php

class Familymember_AdminManageController extends Core_Controller_Action_Admin {
	public function indexAction() {
		$table = Engine_Api::_ ()->getDbTable ( 'type', 'familymember' );
		$this->view->types = $types = $table->fetchAll ();
		
		$this->view->form = $form = new Familymember_Form_Admin_Type ();
		
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$values = $form->getValues ();
			$row = $table->createRow ();
			$row->setFromArray ( $values );
			$row->save ();
			
			$form->addNotice ( Zend_Registry::get ( 'Zend_Translate' )->_ ( 'New type added.' ) );
			
			$this->view->types = $types = $table->fetchAll ();
			
			$db->commit ();
		} catch ( Exception $e ) {
			$form->addError ( $e->getMessage () );
			;
		}
	
	}
	
	public function deleteAction() {
		$table = Engine_Api::_ ()->getDbTable ( 'type', 'familymember' );
		$this->view->types = $types = $table->fetchAll ();
		
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$row = $table->fetchRow ( "type_id = " . $this->_getParam ( 'id', null ) );
			$row->delete ();
			
			$db->commit ();
			
			$this->_helper->redirector->gotoRoute ( array ('action' => 'index' ) );
		} catch ( Exception $e ) {
			throw $e;
		}
	
	}

}
